package com.foreveross.chameleon.manager;

import com.foreveross.chameleon.config.ChameleonConstants;
import com.foreveross.chameleon.exception.NotInstanceException;
import com.foreveross.chameleon.model.ModuleInfo;

import android.content.Context;
import android.content.Intent;

/**
 * @description use to send simple broadcast
 * @author zhouzhineng
 *
 */
public class BroadcastManager {

	private static BroadcastManager mInstance;
	protected Context mContext;
	
	public static void globalInit(Context ctx) {
		if(mInstance == null) {
			mInstance = new BroadcastManager(ctx);
		}
	}
	
	public static BroadcastManager getInstance() throws NotInstanceException {
		if(mInstance == null) {
			throw new RuntimeException("you must call globalInit() before you call this function!");
		}
		return mInstance;
	}
	
	private BroadcastManager(Context ctx) {
		mContext = ctx;
	}
	
	public void send(String action) {
		Intent it = new Intent();
		it.setAction(action);
		mContext.sendBroadcast(it);
	}
	
	/**下载模块时刷新进度条
	 */
	public void sendModuleUpdateProgress(ModuleInfo module, int progress) {
		Intent it = new Intent(ChameleonConstants.ACTION_MODULE_DOWNLOAD_PROGRESS_UPDATE);
		it.putExtra("identify", module.getIdentifier());
		it.putExtra("progress", progress);
		mContext.sendBroadcast(it);
	}
	
	public void sendAppUpdateProgress(int progress) {
		Intent it = new Intent(ChameleonConstants.ACTION_APP_DOWNLOAD_PROGRESS_UPDATE);
		it.putExtra("progress", progress);
		mContext.sendBroadcast(it);
	}
	
	public void sendModuleDownloadFail(ModuleInfo module) {
		Intent it = new Intent(ChameleonConstants.ACTION_MODULE_DOWNLOAD_ERROR);
		it.putExtra("identify", module.getIdentifier());
		mContext.sendBroadcast(it);
	}
	
	public void sendAppDownloadFail() {
		Intent it = new Intent(ChameleonConstants.ACTION_APP_DOWNLOAD_ERROR);
		mContext.sendBroadcast(it);
	}
	
	public void sendAppDownloadStart() {
		send(ChameleonConstants.ACTION_APP_DOWNLOAD_START);
	}
	
	public void sendAppDownloadFinish() {
		send(ChameleonConstants.ACTION_APP_DOWNLOAD_FINISH);
	}
	
	public void sendAppDownloadCancel() {
		send(ChameleonConstants.ACTION_APP_DOWNLOAD_CANCEL);
	}
	
	public void sendModuleDownloadStart(ModuleInfo module){
		Intent it = new Intent(ChameleonConstants.ACTION_MODULE_DOWNLOAD_START);
		it.putExtra("identify", module.getIdentifier());
		mContext.sendBroadcast(it);
	}
	
	public void sendModuleDownloadFinsh(ModuleInfo module){
		Intent it = new Intent(ChameleonConstants.ACTION_MODULE_DOWNLOAD_FINISH);
		it.putExtra("identify", module.getIdentifier());
		mContext.sendBroadcast(it);
	}
	
	public void sendModuleDownloadCancel(ModuleInfo module) {
		Intent it = new Intent(ChameleonConstants.ACTION_MODULE_DOWNLOAD_CANCEL);
		it.putExtra("identify",module.getIdentifier());
		mContext.sendBroadcast(it);
	}
	
	
	public void sendUpdateDownloadCount(int downloadedCount) {
		Intent it = new Intent(ChameleonConstants.ACTION_DOWNLOAD_COUNT_UPDATE);
		it.putExtra(ChameleonConstants.DOWNLOADED_COUNT, downloadedCount);
		mContext.sendBroadcast(it);
	}
	
	public void sendModuleInstallState(String identify,boolean state) {
		Intent it = new Intent(ChameleonConstants.ACTION_MODULE_INSTALL);
		it.putExtra("identify", identify);
		it.putExtra(ChameleonConstants.MODULE_INSTALL_STATE, state);
		mContext.sendBroadcast(it);
	}
	
	public void sendModuleUninstallState(String identify,boolean state) {
		Intent it = new Intent(ChameleonConstants.ACTION_MODULE_UNINSTALL);
		it.putExtra("identify", identify);
		it.putExtra(ChameleonConstants.MODULE_UNINSTALL_STATE, state);
		mContext.sendBroadcast(it);
	}
	
	public void sendModuleUpgradeState(String identify,boolean state) {
		Intent it = new Intent(ChameleonConstants.ACTION_MODULE_UPGRADE);
		it.putExtra("identify", identify);
		it.putExtra(ChameleonConstants.MODULE_UPGRADE_STATE, state);
		mContext.sendBroadcast(it);
	}
	
	public void sendNetworkState(boolean state) {
		Intent it = new Intent(ChameleonConstants.NETWORK_CONNECTED_STATE);
		it.putExtra(ChameleonConstants.NETWORK_STATE, state);
		mContext.sendBroadcast(it);
	}
}
